package com.xhj.xmly.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xhj.xmly.R;
import com.xhj.xmly.activity.MainActivity;
import com.xhj.xmly.adapter.DiscoverAdapter;
import com.xhj.xmly.bean.SecondRecommnedAlbums;
import com.xhj.xmly.callback.HttpService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendContentFragment extends Fragment implements View.OnClickListener, ViewPager.OnPageChangeListener, Callback<SecondRecommnedAlbums> {


    private ImageView coverLarge;
    private TextView title;
    private TextView nickname;
    private TextView playtimes;
    private TextView update;

    public RecommendContentFragment() {
        // Required empty public constructor
    }

    public static RecommendContentFragment newInstance(String id) {

        Bundle args = new Bundle();
        args.putString("id",id);
        RecommendContentFragment fragment = new RecommendContentFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_recommend_content, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        String id = bundle.getString("id");
        new Retrofit.Builder().baseUrl("http://mobile.ximalaya.com/mobile/")
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(HttpService.class)
                .getRecommendContent(id,"android","null",20)
                .enqueue(this);

        coverLarge = (ImageView) view.findViewById(R.id.AlbumDeTop_IV);
        title = (TextView) view.findViewById(R.id.AlbumDeTopName_TV);
        nickname = (TextView) view.findViewById(R.id.AlbumDeTopAn_TV);
        playtimes = (TextView) view.findViewById(R.id.AlbumDeTopPlay_TV);
        update = (TextView) view.findViewById(R.id.AlbumDeTopSta_TV);

        ViewPager viewPager = (ViewPager) view.findViewById(R.id.content_viewpager);
        viewPager.addOnPageChangeListener(this);
        ImageView imageView = (ImageView) view.findViewById(R.id.content_back_image);
        imageView.setOnClickListener(this);
        AlbumDetailFragment albumDetailFragment = new AlbumDetailFragment();
        AlbumShowFragment albumShowFragment = new AlbumShowFragment();
        List<Fragment> list = new ArrayList<>();
        list.add(albumDetailFragment);
        list.add(albumShowFragment);
        String[] titles = new String[]{"详情","节目"};
        DiscoverAdapter adapter = new DiscoverAdapter(getChildFragmentManager(), list, getContext(), titles);
        viewPager.setAdapter(adapter);

    }

    @Override
    public void onClick(View v) {
        MainActivity activity = (MainActivity) getActivity();
        DrawerLayout drawerLayout = activity.getDrawerLayout();
        drawerLayout.closeDrawers();
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        MainActivity activity = (MainActivity) getActivity();
        DrawerLayout drawerLayout = activity.getDrawerLayout();
        if(position==1){
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
        }
        else {
            drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNDEFINED);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onResponse(Call<SecondRecommnedAlbums> call, Response<SecondRecommnedAlbums> response) {
        SecondRecommnedAlbums.DataBean.AlbumBean album = response.body().getData().getAlbum();
        Picasso.with(getContext()).load(album.getCoverLarge()).into(coverLarge);
        title.setText(album.getTitle());
        nickname.setText(album.getNickname());
        playtimes.setText(String.format(Locale.getDefault(),"%.1f万人 ",album.getPlayTimes()/10000.0));
        update.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date(album.getUpdatedAt()))+"更新");
    }

    @Override
    public void onFailure(Call<SecondRecommnedAlbums> call, Throwable t) {

    }
}
